﻿namespace IntegrationTests.ValueConversionTests
{
    using System;
    using System.Collections.ObjectModel;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;

    public abstract class SemanticValueConversionTest : LdapTest, IDisposable
    {
        private static readonly InitialSessionState InitialSessionState = InitialSessionState.CreateDefault();
        private static Runspace runspace;
        

        protected SemanticValueConversionTest() 
        {
            InitializePowerShell();
        }

        private static void InitializePowerShell()
        {
            InitialSessionState.ImportPSModule(new[] { "ActiveDirectory" });
            runspace = RunspaceFactory.CreateRunspace(InitialSessionState);
            runspace.Open();
        }

        protected static Collection<PSObject> ExecutePowerShellScript(string scriptText)
        {
            var command = new Command(scriptText, true);
            var pipeline = runspace.CreatePipeline();
            pipeline.Commands.Add(command);
            return pipeline.Invoke();
        }

        protected static DateTime TrimMilliseconds(DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0);
        }

        public void Dispose()
        {
            runspace.Close();
        }
    }
}
